package com.example.myapplication

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.text.font.FontStyle
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContent {
            MaterialTheme {
                AncientPoetryScreen()
            }
        }
    }
}

@Composable
fun AncientPoetryScreen() {
    // 古风卷轴背景渐变 - 模拟宣纸色调
    val backgroundGradient = Brush.verticalGradient(
        colors = listOf(
            Color(0xFFE8DCCA),
            Color(0xFFF2E8D8),
            Color(0xFFE8DCCA)
        )
    )

    // 卷轴装饰图案（用Box绘制，无需图片资源）
    @Composable
    fun ScrollDecoration(shape: Shape, height: Int) {
        Box(
            modifier = Modifier
                .fillMaxWidth()
                .height(height.dp)
                .background(
                    color = Color(0xFFD4B59E),
                    shape = shape
                )
                .padding(horizontal = 30.dp)
        ) {
            // 卷轴装饰线
            repeat(5) { i ->
                Box(
                    modifier = Modifier
                        .align(Alignment.Center)
                        .height(4.dp)
                        .width(15.dp)
                        .background(Color(0xFFA67C52))
                        .padding(horizontal = 8.dp)
                        .offset(x = (i * 40 - 80).dp)
                )
            }
        }
    }

    Box(
        modifier = Modifier
            .fillMaxSize()
            .background(backgroundGradient)
            .padding(24.dp)
    ) {
        // 顶部卷轴装饰
        ScrollDecoration(
            shape = RoundedCornerShape(topStart = 8.dp, topEnd = 8.dp),
            height = 30
        )

        // 底部卷轴装饰
        ScrollDecoration(
            shape = RoundedCornerShape(bottomStart = 8.dp, bottomEnd = 8.dp),
            height = 30
        )

        // 诗词内容卡片 - 模拟卷轴中间部分
        Card(
            modifier = Modifier
                .fillMaxSize()
                .padding(vertical = 30.dp)
                .align(Alignment.Center),
            elevation = CardDefaults.cardElevation(4.dp),
            colors = CardDefaults.cardColors(containerColor = Color(0xFFF5EFE0)),
            shape = RoundedCornerShape(0.dp)
        ) {
            Column(
                modifier = Modifier
                    .fillMaxSize()
                    .padding(horizontal = 40.dp, vertical = 20.dp)
                    .verticalScroll(androidx.compose.foundation.rememberScrollState()),
                horizontalAlignment = Alignment.CenterHorizontally,
                verticalArrangement = Arrangement.Top
            ) {
                // 诗词标题
                Text(
                    text = "静夜思",
                    fontSize = 36.sp,
                    color = Color(0xFF8B4513),
                    textAlign = TextAlign.Center,
                    modifier = Modifier.padding(bottom = 16.dp)
                )

                // 作者
                Text(
                    text = "唐·李白",
                    fontSize = 20.sp,
                    color = Color(0xFF8B4513),
                    fontStyle = FontStyle.Italic,
                    modifier = Modifier.padding(bottom = 32.dp)
                )

                // 诗词内容
                val poemLines = listOf(
                    "床前明月光，",
                    "疑是地上霜。",
                    "举头望明月，",
                    "低头思故乡。"
                )

                poemLines.forEach { line ->
                    Text(
                        text = line,
                        fontSize = 28.sp,
                        color = Color(0xFF5D4037),
                        lineHeight = 48.sp,
                        textAlign = TextAlign.Center,
                        modifier = Modifier.padding(bottom = 8.dp)
                    )
                }

                Spacer(modifier = Modifier.height(40.dp))

                // 诗词注解
                Text(
                    text = "注解：此诗描写秋日夜晚，诗人于屋内抬头望月而思念故乡的情景。\n语言清新朴素而韵味无穷，历来广为传诵。",
                    fontSize = 16.sp,
                    color = Color(0xFF6D4C41),
                    textAlign = TextAlign.Center,
                    lineHeight = 28.sp,
                    modifier = Modifier.padding(top = 16.dp)
                )

                // 古风印章效果（用Box绘制）
                Box(
                    modifier = Modifier
                        .size(60.dp)
                        .background(Color(0xFF8B0000).copy(alpha = 0.8f))
                        ) {
                    Text(
                        text = "诗",
                        color = Color.White,
                        fontSize = 24.sp,
                        modifier = Modifier.align(Alignment.Center)
                    )
                }
            }
        }
    }
}

@Preview(showBackground = true, name = "古风诗词预览")
@Composable
fun AncientPoetryPreview() {
    MaterialTheme {
        AncientPoetryScreen()
    }
}